#include "workManager.h"
#include <iostream>

WorkManager::WorkManager()
{
    this->m_EmoNumv = 0;
    this->m_Emparry = NULL;
}

void WorkManager::Show_Menu()
{
    cout << "\033c";
    cout << "***************************" << endl;
    cout << "**********welcom***********" << endl;
    cout << "**********0.quit***********" << endl;
    cout << "**********1.addnew*********" << endl;
    cout << "**********2.showall********" << endl;
    cout << "**********3.delectsome*****" << endl;
    cout << "**********4.change*********" << endl;
    cout << "**********5.find***********" << endl;
    cout << "**********6.sort***********" << endl;
    cout << "**********7.clean**********" << endl;
    cout << "***************************" << endl;
    cout << endl;
}

void WorkManager::ExitSystem()
{
    cout << "Welcom to use next time" << endl;
    getchar();
    getchar();
    exit(0);
}
void WorkManager::Add_Emp() 
{
    cout << "input the number of new employee " << endl;

    int addNum = 0;
    cin >> addNum;

    if (addNum>0)
    {
        int newSize = this->m_EmoNumv + addNum;

        Worker** newSpace = new Worker*[newSize];

        if(this->m_Emparry != NULL)
        {
            for (int i = 0; i < this->m_EmoNumv; i++) {
                newSpace[i] = this->m_Emparry[i];
            }
        }

        for (int i = 0; i < addNum; i++)
        {
            int id;
            string name;
            int dSelect;

            cout << "put in the " << i + 1 << " employee'number" << endl;
            cin >> id;

            cout << "put in the " << i + 1 << " employee'name" << endl;
            cin >> name;

            cout << "putin the didof he" << endl;
            cout << "1.employee" << endl
                 << "2.manager" << endl
                 << "3.boss" << endl;
            cin >> dSelect;

            Worker *newwork = NULL;
            switch(dSelect)
            {
                case 1:
                    newwork = new Employee(id, name, 1);
                    break;
                case 2:
                    newwork = new Manager(id, name, 2);
                    break;
                case 3:
                    newwork = new Boss(id, name, 3);
                    break;
            }
            newSpace[this->m_EmoNumv + i] = newwork;
        }
        delete[] this->m_Emparry;
        this->m_Emparry = newSpace;
        this->m_EmoNumv = newSize;

        cout << "comprlrte! " << addNum << " is added!" << endl;

        this->save();

        getchar();

    }
    else
    {
        cout << "wrong number" << endl;
        char s[1024];
        fgets(s, 1024, stdin);
    }
}
void WorkManager::save()
{
    ofstream ofs;
    ofs.open(FILENAME, ios::out|ios::app);

    for(int i = 0;i<this->m_EmoNumv;i++)
    {
        ofs << this->m_Emparry[i]->m_ID
            << " "
            << this->m_Emparry[i]->m_Name
            << " "
            << this->m_Emparry[i]->m_DeptId << endl;

        ofs.close();
    }
}

WorkManager::~WorkManager()
{
    if(this->m_Emparry!=NULL)
    {
        delete[] this->m_Emparry;
        this->m_Emparry = NULL;
    }
}
